package com.huangsm.book.designmodel.concurrentdatastructure;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 多线程对List的只读访问
 * @author huang
 * @date 2019/2/19
 */
public class AccessListThread implements Runnable {
    protected String name;
    private List list;
    Random rand=new Random();

    public AccessListThread() {
    }

    public AccessListThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            //每个线程进行500次get()保证高并发
            for (int i = 0; i < 500; i++) {
                getList(rand.nextInt(1000));
                Thread.sleep(rand.nextInt(100));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public Object getList(int i){
        return list.get(i);
    }
}
